//nocopy
$(function(){
	nocopy.run();
});

var nocopy = {
		run: function(){
			this._init_();
			this._start_();
			this._init_events();			
		},
		
		_init_: function(){
			this.contextPath = $("#contextPath").val() || '';
			this.keyType = $("#keyType").val();
		},
		
		_start_: function(){
			this._list_keyword_nocopy();
		},
		
		_init_events: function(){
			$("#new_nocopy_div").dialog({
				offsetLeft: -20,
				offsetTop: -50,
				gatherData: true,
				submit: function(data){
					var url = nocopy.contextPath + "/admin/nocopy/add";
					$.post(url, data, function(result){				
						if(result.indexOf("Done") == 0){
							location.reload(true);
						}else{
							alert(result);
						}						
					});
				}
			});
			
			$("#keyType").change(function(){
				nocopy.keyType = $(this).val();
				nocopy._list_keyword_nocopy();
			});
			
			$("#add_new_a").click(function(){
				$("#new_nocopy_div").dialog('open');
			});
			
			$("#del_cache_a").click(function() {
				var url = nocopy.contextPath + "/admin/nocopy/delete_cache";
				$.get(url, function(result){				
					alert(result);					
				});
			});
		},
		
		_list_keyword_nocopy: function(url){
			url = url || this.contextPath + "/admin/nocopy/list";
			data = "keyType=" + this.keyType;
			$.get(url, data, function(result){
				$("#nocopy_list_div").html(result);
				nocopy._bind_nocopy_events();
			});
		},
		
		_bind_nocopy_events: function(){
			$("#nocopy_table  tr > td > a").click(function(){
				var $this = $(this);
				var id = $this.attr("noid");
				var url = nocopy.contextPath + "/admin/nocopy/delete";
				data = "id=" + id;
				$.post(url, data, function(result){
					if(result.indexOf("Done") == 0){
						location.reload(true);						
					}else{
						alert(result);
					}
				});
			});
		}
};